package main
import "fmt"
type Vertex struct {
	Lat,Long float64
}
var m map[string]Vertex
func main (){
	//demo1
	m = make(map[string]Vertex)//make会返回给定类型的映射，并将其初始化备用
	m["Bell Labs"] = Vertex{
		40.68433, -74.39967,
	}
	fmt.Println(m["Bell Labs"])//{40.68433 -74.39967}

	//demo2
	n := map[string]Vertex{
		"Google": Vertex{
			37.42202,-122.08408,
		},
		"Bell Labs" : Vertex{
			40.68433, -74.39967,
		},
		//demo3
		//若顶级类型只是一个类型名，可以在文法元素中省略它
		//顶级类型：包级别定义的类型，例如上面的Vertex，可以在本包的任何地方使用
		"Google2" : {37.42202,-122.08408},
		"Bell Labs2" : {40.68433, -74.39967},


	}
	for key, value := range n{
		fmt.Println(key,value)
	}
	/*
		Google {37.42202 -122.08408}
		Bell Labs {40.68433 -74.39967}
	*/

	//demo4
	fmt.Println("============================demo4")
	k := make(map[string]int)
	k["Answer"] = 42
	fmt.Println("The value:",k["Answer"])//The value: 42
	k["Answer"] = 48
	fmt.Println("The value:",k["Answer"])//The value: 48
	//删除元素
	delete(k,"Answer")
	fmt.Println("The value:",k["Answer"])//The value: 0
	//通过双赋值检测某个值是否存在 若key在映射中，ok为true；否则为false
	v, ok := k["Answer"]
	//若key不在映射中，那么elem是该映射元素类型的零值
	fmt.Println("The value:",v,"Present?",ok)//The value: 0 Present? false
	
}